<template>
  <div class="vtext">
    <div class="emoji">
      <i class="icon iconfont icon-biaoqing1" @click="state()" @blur="blurState()" tabindex="0">
        <transition class="showbox">
          <div class="emojibox" ref="emojibox" v-show="flag">
            <!--  @blur="blurState()" tabindex="0" -->
            <li v-for="(emoji, key) in emojis" :key="key">
              <img :src="'/emoji/'+emoji.file" :alt="emoji.code" :title="emoji.title" />
            </li>
          </div>
        </transition>
      </i>
    </div>
    <textarea ref="textarea" v-model="content" @keydown.enter="onKeyup"></textarea>
    <div class="send" @click="send()">
      <span>发送(S)</span>
    </div>
    <transition name="appear">
      <div class="warn" v-show="warn">
        <div class="dexcription">不能发送空白信息</div>
      </div>
    </transition>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      flag: false,
      warn: false,
      content: ""
    };
  },
  computed: {
    ...mapState(["emojis"])
  },
  methods: {
    ...mapActions(["sendMessage"]),
    blurState() {
      this.flag = false;
    },
    state() {
      this.flag = true;
      // setTimeout(() => this.$refs.emojibox.focus(), 3);
      // setTimeout(() => this.$refs.textarea.focus(), 0);
    },
    // 回车控制
    onKeyup(e) {
      if (!e.shiftKey) {
        e.preventDefault();
        this.send();
      }
    },
    // 发送消息
    send() {
      if (
        this.content
          .replace(/\r\n/g, "")
          .replace(/\n/g, "")
          .replace(/\s/g, "").length == 0
      ) {
        this.warn = true;
        this.content = "";
        setTimeout(() => this.$refs.textarea.focus(), 3);
        setTimeout(() => {
          this.warn = false;
        }, 1000);
      } else {
        // 发送消息
        this.sendMessage(this.content);
        this.content = "";
      }
    }
  }
};
</script>

<style scoped>
.vtext {
  position: relative;
  height: 150px;
  /* background-color: #fff; */
  background-color: #f2f2f2;
  outline: none;
}

.vtext:focus-within {
  background-color: #fff;
}

.emoji {
  position: relative;
  width: 100%;
  height: 35px;
  line-height: 40px;
  font-size: 12px;
  padding: 0 30px;
  box-sizing: border-box;
  color: #878787;
}

.emoji .icon-biaoqing1 {
  font-size: 18px;
  cursor: pointer;
  outline: none;
}

.emoji .icon-biaoqing1:hover {
  color: #1aad19;
}

.emoji .emojibox {
  position: absolute;
  display: flex;
  flex-wrap: wrap;
  top: -210px;
  left: -100px;
  width: 300px;
  height: 200px;
  padding: 5px;
  background-color: #fff;
  border: 1px solid #d1d1d1;
  border-radius: 2px;
  box-shadow: 0 1px 2px 1px #d1d1d1;
  outline: none;
}

.emoji .emojiBox .showbox-enter-active,
.emoji .emojiBox .showbox-leave-active {
  transition: all 0.5s;
}

.emoji .emojiBox .showbox-enter,
.emoji .emojiBox .showbox-leave-active {
  opacity: 0;
}

textarea {
  box-sizing: border-box;
  padding: 0 30px;
  height: 100px;
  width: 100%;
  border: none;
  outline: none;
  font-family: "Micrsofot Yahei";
  resize: none;
  background-color: transparent;
}

textarea:focus {
  background-color: #fff;
}

.send {
  position: absolute;
  bottom: 10px;
  right: 30px;
  width: 75px;
  height: 28px;
  line-height: 28px;
  box-sizing: border-box;
  text-align: center;
  border: 1px solid #e5e5e5;
  border-radius: 3px;
  background: #f5f5f5;
  font-size: 14px;
  color: #7c7c7c;
}

.send:hover {
  background: rgb(18, 150, 17);
  color: #fff;
}

.warn {
  position: absolute;
  bottom: 50px;
  right: 10px;
  width: 110px;
  height: 30px;
  line-height: 30px;
  font-size: 12px;
  text-align: center;
  border: 1px solid #bdbdbd;
  border-radius: 4px;
  box-shadow: 0 1px 5px 1px #bdbdbd;
}

.warn .appear-enter-active,
.warn .appear-leave-active {
  transition: all 1s;
}

.warn .appear-enter,
.warn .appear-leave-active {
  opacity: 0;
}

.warn:before {
  content: " ";
  position: absolute;
  top: 100%;
  right: 20px;
  border: 7px solid transparent;
  border-top-color: #fff;
  filter: drop-shadow(1px 3px 2px #bdbdbd);
}
</style>